/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.fault;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.Condition;
import com.sinodata.bsm.center.bean.Page;
import com.sinodata.bsm.center.service.fault.TrapService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.vo.FaultTrapMsg;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-11 下午9:51:14          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class TrapAction extends BaseAction {

    private static final Logger logger = Logger.getLogger(TrapAction.class);

    private static final long serialVersionUID = 1L;

    private final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private TrapService trapService;

    public String common() {
        return "common";
    }

    public String rule() {
        Struts2Utils.getRequest().setAttribute("resId", request.getParameter("id"));
        return "rule";
    }

    @Override
    public Object getModel() {
        return null;
    }

    /**
     * 返回trap列表
     */
    @Override
    public String list() throws Exception {
        JSONObject json = new JSONObject();
        String ip = request.getParameter("ip");
        String startTime = request.getParameter("start");
        String endTime = request.getParameter("end");
        JSONArray array = new JSONArray();
        Page<FaultTrapMsg> page = new Page<FaultTrapMsg>();
        Condition condition = new Condition();
        Map<String, String> map = new HashMap<String, String>();
        if (ip != null && ip.length() > 0) {
            map.put("ip", ip);
        }
        if (startTime != null && startTime.length() > 0) {
            map.put("start", startTime);
        }
        if (endTime != null && endTime.length() > 0) {
            map.put("end", endTime);
        }
        condition.setWhere(map);
        page.setPageNo(Integer.valueOf(request.getParameter("iDisplayStart")) / Integer.valueOf(request.getParameter("iDisplayLength")) + 1);
        page.setPageSize(Integer.valueOf(request.getParameter("iDisplayLength")));

        page = trapService.findByPage(page, condition);
        List<FaultTrapMsg> list = page.getRows();
        for (FaultTrapMsg trap : list) {
            JSONObject obj = new JSONObject();
            obj.element("ip", trap.getIp());
            obj.element("time", sf.format(trap.getRecevTime()));
            obj.element("generic", trap.getGenericTrap());
            obj.element("level", trap.getSecLevel());
            obj.element("errostatus", trap.getErrorStatus());
            obj.element("timestamp", trap.getTimeStamp());
            obj.element("comunity", trap.getSecName());
            obj.element("version", trap.getVersion());
            obj.element("msg", "<div class='trap-fit'><label  title='" + trap.getMsg() + "'>" + trap.getMsg() + "</label></div>");
            //            obj.element("msg2", "<div class='msg-fit'>" + trap.getMsg2() + "</div>");
            //            obj.element("msg3", "<div class='msg-fit'>" + trap.getMsg3() + "</div>");
            //            obj.element("msg4", "<div class='msg-fit'>" + trap.getMsg4() + "</div>");
            //            obj.element("msg5", "<div class='msg-fit'>" + trap.getMsg5() + "</div>");
            //            obj.element("msg6", "<div class='msg-fit'>" + trap.getMsg6() + "</div>");
            //            obj.element("msg7", "<div class='msg-fit'>" + trap.getMsg7() + "</div>");
            //            obj.element("msg8", "<div class='msg-fit'>" + trap.getMsg8() + "</div>");
            //            obj.element("msg9", "<div class='msg-fit'>" + trap.getMsg9() + "</div>");
            //            obj.element("msg10", "<div class='msg-fit'>" + trap.getMsg10() + "</div>");
            array.add(obj);
        }
        json.element("iTotalRecords", page.getTotal());
        json.element("iTotalDisplayRecords", page.getTotal());
        json.element("aaData", array);
        Struts2Utils.renderText(json.toString());
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

}
